<?php 

//include('include/singleton-timer.php');

// For timing queries
// $Timer = new SingletonTimer::get_instance();

function marvin_codebase() {
	$marvin_codebase = 'marvin';
	return $marvin_codebase;
}

function home_page() {
	$_ROOT_DIR = "ChemCompDB";
	$url = "http://" . $_SERVER['SERVER_NAME'] . "/" . $_ROOT_DIR;
	return $url;
}

function show_2d_smiles_structure ( $smiles ) {
	echo '<script language="JavaScript">';
	//echo 'mview_param("chiralitySupport", "selected");';
	echo 'mview_begin("' . marvin_codebase() . '", 200, 200);';
	echo 'mview_param("mol","' . $smiles .'");';
	echo 'mview_end();';
	echo '</script>';
}

function show_3d_smiles_structure ( $string ) {
	echo '<script LANGUAGE="JavaScript1.1">';
	echo '<!--';
	echo 'mview_begin("' . marvin_codebase() . '", 200, 200);';
	echo 'mview_param("mol", "'. $string .'");';
	echo 'mview_param("molbg", "#000000");';
	echo 'mview_param("animate", "all");';
	echo 'mview_param("navmode", "rot3d");';
	echo 'mview_param("rendering", "ballstick");';
	echo 'mview_param("animFPS", "20");';
	echo 'mview_end();';
	echo '//-->';
	echo '</script>';
}

function generate_mol2 ( $cid ) {
	$MOL2="";
	
	// MOLECULE
	$q = "SELECT * FROM Molecule_Data WHERE CID = ". $cid .";";
	$header = "@&lt;TRIPOS&gt;MOLECULE\n";
	$result = mysql_query($q);
	if (($data = mysql_fetch_assoc($result))) {
		$MOL2 .= $header;
		$MOL2 .= $data['CID'] . "\n";
		$MOL2 .= $data['ATOM_COUNT'] . " ";
		$MOL2 .= $data['BOND_COUND'] . " ";
		$MOL2 .= $data['SUBSTRUCTURE_COUNT'] . " ";
		$MOL2 .= $data['FEATURE_COUNT'] . " ";
		$MOL2 .= $data['SET_COUNT'] . "\n";
		$MOL2 .= $data['MOLECULE_TYPE'] . "\n"; 
		$MOL2 .= $data['MOLECULE_CHARGE'] . "\n";
	}
	else { return ""; }
	
	//ATOM
	$q = "SELECT * FROM Atom_Data WHERE CID = ". $cid .";";
	$header = "\n@&lt;TRIPOS&gt;ATOM\n";
	$MOL2 .= $header;
	$result = mysql_query($q);
	if ($result) {
		while (($data = mysql_fetch_assoc($result))) {
			$MOL2 .= $data['ATOM_ID'] . " ";
			$MOL2 .= $data['ATOM'] . " ";
			$MOL2 .= $data['X_COORD'] . " ";
			$MOL2 .= $data['Y_COORD'] . " ";
			$MOL2 .= $data['Z_COORD'] . " ";
			$MOL2 .= $data['ATOM_TYPE'] . " ";
			$MOL2 .= $data['SUBSTRUCTURE_ID'] . " ";
			$MOL2 .= $data['SUBSTRUCTURE_NAME'] . " ";
			$MOL2 .= $data['CHARGE'] . "\n";
		}
	}
	else { return ""; } 

	//BOND
	$q = "SELECT * FROM Bond_Data WHERE CID = ". $cid .";";
	$header = "\n@&lt;TRIPOS&gt;BOND\n";
	$MOL2 .= $header;
	$result = mysql_query($q);
	if ($result) {
		while (($data = mysql_fetch_assoc($result))) {
			$MOL2 .= $data['BOND_ID'] . " ";
			$MOL2 .= $data['FIRST_ATOM'] . " ";
			$MOL2 .= $data['SECOND_ATOM'] . " ";
			$MOL2 .= $data['BOND_TYPE'] . "\n";
		}
	}
	else { return ""; }
	
	//MOLECULE
	$q = "SELECT * FROM Substructure_Data WHERE CID = ". $cid .";";
	$header = "\n@&lt;TRIPOS&gt;SUBSTRUCTURE\n";
	$MOL2 .= $header;
	$result = mysql_query($q);
	if ($result) {
		while (($data = mysql_fetch_assoc($result))) {
			$MOL2 .= $data['SUBSTRUCTURE_ID'] . " ";
			$MOL2 .= $data['SUBSTRUCTURE_NAME'] . " ";
			$MOL2 .= $data['ROOT_ATOM_ID'] . " ";
			$MOL2 .= $data['SUBSTRUCTURE_TYPE'] . " ";
			$MOL2 .= $data['DICTIONARY_TYPE'] . " ";
			$MOL2 .= $data['CHAIN'] . " ";
			$MOL2 .= $data['CHAIN_SUBTYPE'] . " ";
			$MOL2 .= $data['INTERSUBSTRUCTURE_BONDS'] . "\n";
		}
	}
	else { return ""; }
	
	return $MOL2;
}

function IUPAC_NAME_query( $ss, $limiters ) {
	$rq = generate_range_query($limiters);
	
	$Q = "SELECT * ";
	$Q .= "FROM Compounds WHERE ". $rq ." PUBCHEM_COMPOUND_CID IN (";
	$Q .= "SELECT CID FROM Compound_SMILES WHERE MATCH(PUBCHEM_OPENEYE_ISO_SMILES) ";
	$Q .= "AGAINST('". $ss ."' IN BOOLEAN MODE))";
	
	return mysql_query($Q);
}

function display_results( $searchString, $rowsPP, $offset ) {
	// to time queries
	$Timer->start_timer();
	
	// search and result code to be moved here
	
	$Timer->end_timer();
}

// Must be pased in order of donor, acceptor, weight, logp and rotatable
// beggining with the lower bound of each parameter
function generate_range_query( $ary ) {
	$ranges = array();
	$size = count($ary);
	// check the lower, if there is a lower check upper
	// if lower and upper do BETWEEN
	// if only lower do GREATER THAN
	// otherwise do LESS THAN
	if (($size % 2) !== 0 ) {
		return ""; // don't apply anything since it will break
	}
	
	// make sure all values are numbers
	for ($n=0; $n < $size; $n++) {
		if (!(is_numeric($ary[$n]))) { $ary[$n] = ""; }
	}
	
	// make sure the lowerbound is greater than the upper
	for ($n=0; $n < $size; $n+=2) {
		if (($ary[$n] !== "") && ($ary[$n+1] !== "")) {
			if ($ary[$n] > $ary[$n+1]) {
				$tmp = $ary[$n+1];
				$ary[$n+1] = $ary[$n];
				$ary[$n] = $tmp;
			}
		}
	}
	
	$index=0;
	// Hydrogen Donors
	if ($ary[$index] !== "") {
		if ($ary[$index+1] !== "") {  
			$ranges[0] = "PUBCHEM_CACTVS_HBOND_DONOR BETWEEN " . $ary[$index] ."AND". $ary[$index+1] . " AND "; 
		}
		else {
			$ranges[0] = "PUBCHEM_CACTVS_HBOND_DONOR > " . $ary[$index] . " AND ";
		}
	}
	else if ($ary[$index+1] !== "") {
		$ranges[0] = "PUBCHEM_CACTVS_HBOND_DONOR < " . $ary[$index+1] . " AND "; 
	} 
	else {
		$ranges[0] = "";
	}

	// Hydrogen Acceptors
	$index += 2;
	if ($ary[$index] !== "") {
		if ($ary[$index+1] !== "") {  
			$ranges[1] = "PUBCHEM_CACTVS_HBOND_ACCEPTOR BETWEEN " . $ary[$index] ." AND ". $ary[$index+1] . " AND ";
		}
		else {
			$ranges[1] = "PUBCHEM_CACTVS_HBOND_ACCEPTOR > " . $ary[$index] . " AND "; 
		}
	}
	else if ($ary[$index+1] !== "") {
		$ranges[1] = "PUBCHEM_CACTVS_HBOND_ACCEPTOR < " . $ary[$index+1] . " AND "; 
	} 
	else {
		$ranges[1] = "";
	}

	// Molecular Weight
	$index += 2;
	if ($ary[$index] !== "") {
		if ($ary[$index+1] !== "") {  
			$ranges[2] = "PUBCHEM_MOLECULAR_WEIGHT BETWEEN " . $ary[$index] ." AND ". $ary[$index+1] . " AND "; 
		}
		else {
			$ranges[2] = "PUBCHEM_MOLECULAR_WEIGHT > " . $ary[$index] . " AND "; 
		}
	}
	else if ($ary[$index+1] !== "") {
		$ranges[2] = "PUBCHEM_MOLECULAR_WEIGHT < " . $ary[$index+1] . " AND "; 
	} 
	else {
		$ranges[2] = "";
	}

	// Log P
	$index += 2;
	if ($ary[$index] !== "") {
		if ($ary[$index+1] !== "") {  
			$ranges[3] = "PUBCHEM_XLOGP3_AA BETWEEN " . $ary[$index] ." AND ". $ary[$index+1] . " AND "; 
		}
		else {
			$ranges[3] = "PUBCHEM_XLOGP3_AA > " . $ary[$index] . " AND "; 
		}
	}
	else if ($ary[$index+1] !== "") {
		$ranges[3] = "PUBCHEM_XLOGP3_AA < " . $ary[$index+1] . " AND "; 
	} 
	else {
		$ranges[3] = "";
	}
	
	// Rotatable Bonds
	$index += 2;
	if ($ary[$index] !== "") {
		if ($ary[$index+1] !== "") {  
			$ranges[4] = "PUBCHEM_CACTVS_ROTATABLE_BOND BETWEEN " . $ary[$index] ." AND ". $ary[$index+1] . " AND "; 
		}
		else {
			$ranges[4] = "PUBCHEM_CACTVS_ROTATABLE_BOND > " . $ary[$index] . " AND "; 
		}
	}
	else if ($ary[$index+1] !== "") {
		$ranges[4] = "PUBCHEM_CACTVS_ROTATABLE_BOND < " . $ary[$index+1] . " AND "; 
	} 
	else {
		$ranges[4] = "";
	}
	
	// ordered by ability to limit the query based on range and uniformity
	// logP, acceptor, rotatable, mol weight, donor,
	$rString = $ranges[3] . " " . $ranges[1] . " " . $ranges[4] . " " . $ranges[2] . " " . $ranges[0];
	
	if ($rString == "    ") { $rString = ""; }
	// remove trailing 'AND' and return string
	$pattern = '/[ ]+AND[ ]+$/';
	return 	preg_replace($pattern, "", $rString, 1);
}

?>
